//
//  FictitiousOrderConfirmControl.m
//  ai2ban
//
//  Created by Heartbeats on 2018/1/13.
//  Copyright © 2018年 mac book pro. All rights reserved.
//

#import "FictitiousOrderConfirmControl.h"

@interface FictitiousOrderConfirmControl ()
@property (weak, nonatomic) IBOutlet UIImageView *goodImgV;
@property (weak, nonatomic) IBOutlet UILabel *goodName;
@property (weak, nonatomic) IBOutlet UILabel *goodScoreLab;
@property (weak, nonatomic) IBOutlet UITextField *userNameTextFile;
@property (weak, nonatomic) IBOutlet UITextField *phoneTextFile;

@end

@implementation FictitiousOrderConfirmControl

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setGoodModel:self.goodModel];
}

- (IBAction)exChangeAction:(UIButton *)sender {
    if ([self.userNameTextFile.text length] == 0) {
        [MozTopAlertView showWithType:1 text:@"姓名不能为空" parentView:nil];
        return;
    }
    if (![self checkPhone:self.phoneTextFile.text]) {
        [MozTopAlertView showWithType:1 text:@"请确认手机号正确性" parentView:nil];
        return;
    }
    sender.enabled = NO;
    NSDictionary *param = @{@"userId": AppManager.userId, @"orderId": [self.orderInfoModel[@"orderId"] stringValue]};
    NSArray *keyArray = [param allKeys];
    NSString *paramStr = @"";
    for (NSString *key in  [keyArray sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)]) {
        paramStr = [NSString stringWithFormat:@"%@%@=%@&",paramStr, key, [param objectForKey:key]];
    }
    paramStr = [NSString stringWithFormat:@"%@%@=%@&", paramStr, @"srcKey", @"NFRs4PxuGQFIot3T"];
    NSMutableDictionary *paramDict = [NSMutableDictionary dictionaryWithDictionary:param];
    [paramDict setObject:[NSString md5:paramStr] forKey:@"MD5"];
    [[NetworkManager shareNetwork] requestWithURLString:@"app/system/payOrder" parameters:paramDict method:@"POST" callBack:^(id responseObject) {
        sender.enabled = true;
        if ([responseObject[@"data"] intValue]) {
            [MozTopAlertView showWithType:1 text:@"兑换成功" parentView:nil];
            [[NSNotificationCenter defaultCenter] postNotificationName:UpdateUserDataNotification object:nil];
            [self.navigationController popToRootViewControllerAnimated:true];
            return;
        }
        [MozTopAlertView showWithType:1 text:responseObject[@"data"] parentView:nil];
    }];
}

- (void)setGoodModel:(JiFenGoodModel *)goodModel {
    _goodModel = goodModel;
    self.goodName.text = goodModel.name;
    self.goodScoreLab.text = goodModel.showScore;
    [self.goodImgV sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",ImageBaseUrl, goodModel.picImg]]];
    self.userNameTextFile.text = AppManager.user.userName;
    self.phoneTextFile.text = AppManager.user.telephone;
}
- (BOOL) checkPhone:(NSString *)phone {
    NSString *MOBILE = @"^1(3[0-9]|4[57]|5[0-35-9]|8[0-9]|70)\\d{8}$";
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
    return [regextestmobile evaluateWithObject:phone];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
